package top.lyjwn.ajui.util.store;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * 文件存储
 *
 * @author luoyujie
 */
public class FileStoreStringUtil {

    public static void saveStringToFile(File file, String content) {
        try {
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println(content);
            ps.close();
        } catch (FileNotFoundException e) {
            Log.e("saveStringToFileFail", "read fail " + e.getMessage() + "");
        }

    }

    public static String readStringByFile(File file) {
        String encoding = "UTF-8";
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            in.read(filecontent);
            return new String(filecontent, encoding);
        } catch (Exception e) {
            Log.e("readStringByFileFail", "read fail " + e.getMessage() + "");
            return null;
        } finally {
            try {
                if (in!=null){
                    in.close();
                }
            } catch (IOException e) {
                Log.e("readStringByFileFail", "close fail " + e.getMessage() + "");
            }
        }
    }

}
